//ACV_functions_dlg.sqf
#include "ACV_constantes.h"
#include "ACV_ID_main_dlg.h"
#include "ACV_ID_wp_dlg.h"
ACV_InitPlayer = {
	player addAction ["A.C.V" call R3F_FNCT_Red,"R3F_ACV\Scripts\show_main_dlg.sqf","",-10,false,false,"","ACV_User"];
};
ACV_OnClose = {
	ACV_FlashMessage = false;
	ACV_MARKER_LOC_Unit setMarkerPosLocal [0,0,0];
	ACV_MARKER_LOC_Wp setMarkerPosLocal [0,0,0];
};
ACV_FNCT_FlashMessage = {
	private ["_control","_msg","_show","_start"];
	disableSerialization;
	_control = _this select 0;
	_msg = _this select 1;
	_show = true;
	ACV_FlashMessage = true;
	_start = time;
	while {ACV_FlashMessage} do {
		if (_show) then {
			_control ctrlSetText _msg;
		} else {
			_control ctrlSetText " ";
		};
		_show = !_show;
		sleep 0.300;
		if ((time - _start) > 30) then {
			call ACV_FNCT_CancelMakeUnit;
			ACV_FlashMessage = false;
			_control ctrlSetText " ";
			ACV_Void = true;
			ACV_Go = false;
		};
	};
};
ACV_FNCT_SysMessage = {
	private "_txt_sys";
	disableSerialization;
	_txt_sys = _display displayCtrl ACV_DLG_ID_Main_TxtSystem;
	_txt_sys ctrlSetText (_this select 0);
};
ACV_FNCT_MakeUnit = {
	ctrlEnable [ACV_DLG_ID_Main_CbCamp,false];
	ctrlEnable [ACV_DLG_ID_Main_CbFaction,false];
	ctrlEnable [ACV_DLG_ID_Main_CbType,false];
	ctrlEnable [ACV_DLG_ID_Main_CbUnit,false];
	ctrlEnable [ACV_DLG_ID_Main_SliderSkill,false];
	ctrlEnable [ACV_DLG_ID_Main_SliderDir,false];
	ctrlEnable [ACV_DLG_ID_Main_ChkEmpty,false];
	ctrlEnable [ACV_DLG_ID_Main_EnVol,false];
	ctrlEnable [ACV_DLG_ID_Main_BtnMakeUnit,false];
	ctrlEnable [ACV_DLG_ID_Main_GrpCbTypeUnit,false];
	ctrlEnable [ACV_DLG_ID_Main_GrpCbUnit,false];
	ctrlEnable [ACV_DLG_ID_Main_BtnMakeGrp,false];
	ctrlEnable [ACV_DLG_ID_Main_CbObjtype,false];
	ctrlEnable [ACV_DLG_ID_Main_MkObj,false];
	ctrlEnable [ACV_DLG_ID_Main_LstStock,false];
	ctrlEnable [ACV_DLG_ID_Main_BtnDeleteUnit,false];
	ctrlEnable [ACV_DLG_ID_Main_CbShowAll,false];
	ctrlEnable [ACV_DLG_ID_Main_ListObjects,false];
	ctrlEnable [ACV_DLG_ID_Main_DeleteObjects,false];
	ctrlEnable [ACV_DLG_ID_Main_ListWp,false];
	ctrlEnable [ACV_DLG_ID_Main_AddWp,false];
	ctrlEnable [ACV_DLG_ID_Main_EditWp,false];
	ctrlEnable [ACV_DLG_ID_Main_DelWp,false];
	ctrlEnable [ACV_DLG_ID_Main_BtnCamera,false];
	ctrlEnable [ACV_DLG_ID_Main_BtnTeleport,false];
	ctrlEnable [ACV_DLG_ID_Main_BtnScript,false];
	ctrlEnable [ACV_DLG_ID_Main_BtnMoveAuto,false];
	ctrlEnable [ACV_DLG_ID_Main_CbListPlayers,false];
	ctrlEnable [ACV_DLG_ID_Main_BtnCancelMakeUnit,true];
};
ACV_FNCT_CancelMakeUnit = {
	ctrlEnable [ACV_DLG_ID_Main_CbCamp,true];
	ctrlEnable [ACV_DLG_ID_Main_CbFaction,true];
	ctrlEnable [ACV_DLG_ID_Main_CbType,true];
	ctrlEnable [ACV_DLG_ID_Main_CbUnit,true];
	ctrlEnable [ACV_DLG_ID_Main_SliderSkill,true];
	ctrlEnable [ACV_DLG_ID_Main_SliderDir,true];
	ctrlEnable [ACV_DLG_ID_Main_ChkEmpty,true];
	ctrlEnable [ACV_DLG_ID_Main_EnVol,true];
	ctrlEnable [ACV_DLG_ID_Main_BtnMakeUnit,true];
	ctrlEnable [ACV_DLG_ID_Main_GrpCbTypeUnit,true];
	ctrlEnable [ACV_DLG_ID_Main_GrpCbUnit,true];
	ctrlEnable [ACV_DLG_ID_Main_BtnMakeGrp,true];
	ctrlEnable [ACV_DLG_ID_Main_CbObjtype,true];
	ctrlEnable [ACV_DLG_ID_Main_MkObj,true];
	ctrlEnable [ACV_DLG_ID_Main_LstStock,true];
	ctrlEnable [ACV_DLG_ID_Main_BtnDeleteUnit,((lbCurSel ACV_DLG_ID_Main_LstStock) >= 0)];
	ctrlEnable [ACV_DLG_ID_Main_CbShowAll,true];
	ctrlEnable [ACV_DLG_ID_Main_ListObjects,true];
	ctrlEnable [ACV_DLG_ID_Main_DeleteObjects,((lbCurSel ACV_DLG_ID_Main_ListObjects) >= 0)];
	ctrlEnable [ACV_DLG_ID_Main_ListWp,((lbCurSel ACV_DLG_ID_Main_LstStock) >= 0)];
	ctrlEnable [ACV_DLG_ID_Main_AddWp,((lbCurSel ACV_DLG_ID_Main_LstStock) >= 0)];
	ctrlEnable [ACV_DLG_ID_Main_EditWp,((lbCurSel ACV_DLG_ID_Main_ListWp) >= 0)];
	ctrlEnable [ACV_DLG_ID_Main_DelWp,((lbCurSel ACV_DLG_ID_Main_ListWp) > 0)];
	ctrlEnable [ACV_DLG_ID_Main_BtnCamera,true];
	ctrlEnable [ACV_DLG_ID_Main_BtnTeleport,true];
	ctrlEnable [ACV_DLG_ID_Main_BtnScript,true];
	ctrlEnable [ACV_DLG_ID_Main_BtnMoveAuto,true];
	ctrlEnable [ACV_DLG_ID_Main_CbListPlayers,true];
	ctrlEnable [ACV_DLG_ID_Main_BtnCancelMakeUnit,false];
	ACV_Void = true;
	ACV_Go = false;
};
ACV_FNCT_GetFrenchMarker = {
	private ["_name","_return"];
	_name = (_this select 0);
	_return = "AUCUN";
	{	if (_name == (_x select 0)) then {
			_return = (_x select 1),
		};
	} foreach ACV_WAYPOINTARRAY;
	_return;
};
ACV_FNCT_ClearAll_WpMarkers = {
	ACV_MARKER_LOC_Wp setMarkerPosLocal [0,0,0];
};
ACV_FNCT_Clear_WpMarker = {
	ACV_MARKER_LOC_Wp setMarkerPosLocal [0,0,0];
};
ACV_FNCT_Show_WpMarker = {
	private ["_pos","_strtype"];
	_pos = _this select 0;
	_strtype = _this select 1;
	ACV_MARKER_LOC_Wp setMarkerPosLocal [(_pos select 0),(_pos select 1)];
	ACV_MARKER_LOC_Wp setMarkerShapeLocal "ICON";
   	ACV_MARKER_LOC_Wp setMarkerTypeLocal "mil_circle";
   	ACV_MARKER_LOC_Wp setMarkerSizeLocal [0.5,0.5];
   	ACV_MARKER_LOC_Wp setMarkerTextLocal _strtype;
  	ACV_MARKER_LOC_Wp setMarkerColorLocal "ColorYellow";
  	ACV_MARKER_LOC_Wp setMarkerAlphaLocal 0.4;
};
ACV_FNCT_BroadcastMessage = {
	private "_msg";
	if (ACV_Broadcast_Message) then {
		_msg = [];
		_msg set [ACV_BROADCAST_PARAM_TYPE,ACV_BROADCAST_TYPE_MSG];
		_msg set [ACV_BROADCAST_PARAM_MSG,(_this select 0)];
		_msg set [ACV_BROADCAST_PARAM_TYPE_DEST,ACV_Broadcast_MsgACV];
		_msg set [ACV_BROADCAST_PARAM_DEST,''];
		_msg set [ACV_BROADCAST_PARAM_ARGS, objNull];
		ACV_NET_BroadcastMSG = _msg;
     	publicVariable "ACV_NET_BroadcastMSG";
     	[_msg] call ACV_FNCT_ShowMessage;
	};
};
ACV_FNCT_BroadcastCode = {
	private ["_msg","_args","_dest"];
	if (count _this > 2) then {
		_args = _this select 2;
	} else {
		_args = objNull;
	};
	if (count _this > 3) then {
		_dest = _this select 3;
	} else {
		_dest = "";
	};
	_msg = [];
	_msg set [ACV_BROADCAST_PARAM_TYPE,ACV_BROADCAST_TYPE_CODE];
	_msg set [ACV_BROADCAST_PARAM_TYPE_DEST,(_this select 0)];
	_msg set [ACV_BROADCAST_PARAM_MSG,(_this select 1)];
	_msg set [ACV_BROADCAST_PARAM_ARGS, _args];
	_msg set [ACV_BROADCAST_PARAM_DEST,_dest];
	ACV_NET_BroadcastMSG = _msg;
   	publicVariable "ACV_NET_BroadcastMSG";
	[_msg] call ACV_FNCT_ShowMessage;
};
ACV_FNCT_ShowMessage = {
	private ["_params","_msg_type","_msg","_dest_type","_dest","_args","_to_send","_display"];
	diag_log Format["ACV ShowMessage: %1",_this];
	_params = _this select 0;
	_msg_type = _params select ACV_BROADCAST_PARAM_TYPE;
	_msg = _params select ACV_BROADCAST_PARAM_MSG;
	_dest_type = _params select ACV_BROADCAST_PARAM_TYPE_DEST;
	_dest = _params select ACV_BROADCAST_PARAM_DEST;
	_args = _params select ACV_BROADCAST_PARAM_ARGS;
	_to_send = false;
	if (_dest_type == ACV_BROADCAST_TO_ALL) then {
		_to_send = true;
	};
	if (_dest_type == ACV_BROADCAST_TO_ACV) then {
		if (ACV_User) then {
			_to_send = true;
		};
	};
	if (_dest_type == ACV_BROADCAST_TO_ONE) then {
		if (player == _dest) then {
			_to_send = true;
		};
	};
	if (_dest_type == ACV_BROADCAST_TO_SERVER) then {
		if (isServer) then {
			_to_send = true;
		};
	};
	if (_dest_type == ACV_BROADCAST_TO_CLIENT) then {
		if !(isServer && isDedicated) then {
			_to_send = true;
		};
	};
	if (_to_send) then {
		if (_msg_type == ACV_BROADCAST_TYPE_MSG) then {
			disableSerialization;
			_display = findDisplay ACV_DLG_ID_Main_Dlg;
			if (_display != displayNull) then {
				[_msg] call ACV_FNCT_SysMessage;
			} else {
				player globalChat _msg;
			};
		} else {
			if (_msg_type == ACV_BROADCAST_TYPE_CODE) then {
				_args spawn _msg;
			};
		};
	};
};
ACV_FNCT_RefreshListUnits = {
	private ["_display","_varname","_action","_owner","_list","_old_idx","_index"];
	disableSerialization;
	_display = findDisplay ACV_DLG_ID_Main_Dlg;
	if (_display != displayNull) then {
		if ((count _this) == 3) then {
			_varname = _this select 0;
			_action = _this select 1;
			_owner = _this select 2;
		} else {
			_varname = '';
			_action = -1;
			_owner = name player;
		};
		_list = _display displayCtrl ACV_DLG_ID_Main_LstStock;
		_old_idx = lbCurSel _list;
		lbClear ACV_DLG_ID_Main_LstStock;
		lbClear ACV_DLG_ID_Main_ListWp;
		ACV_Unit_Stock = Server getVariable "ACV_Unit_Stock";
		_index = -1;
		{	_index = _list lbAdd (_x getVariable "Name");
			lbSetData [ACV_DLG_ID_Main_LstStock,_index,(_x getVariable "VarName")];
			if (side _x == west) then {
				_list lbSetColor [_index,[0.3,0.3,0.8,1]];
			} else {
				if (side _x == east) then {
					_list lbSetColor [_index,[0.8,0.2,0.2,1]];
				} else {
					if (side _x == resistance) then {
						_list lbSetColor [_index,[1,0.7,0.2,1]];
					} else {
						_list lbSetColor [_index,[0.2,0.8,0.2,1]];
					};
				};
			};
			if ((count units _x) == 0) then {
				_list lbSetColor [_index,[0.3,0.3,0.3,1]];
			};
		} foreach ACV_Unit_Stock;
		if (_owner != (name player)) then {
			lbSetCurSel [ACV_DLG_ID_Main_LstStock,_old_idx];
		} else {
			lbSetCurSel [ACV_DLG_ID_Main_LstStock,_index];
		};
		ctrlEnable [ACV_DLG_ID_Main_ListWp,_index >= 0];
		ctrlEnable [ACV_DLG_ID_Main_AddWp,_index >= 0];
		ctrlEnable [ACV_DLG_ID_Main_EditWp,_index >= 0];
		ctrlEnable [ACV_DLG_ID_Main_DelWp,_index >= 0];
		ctrlEnable [ACV_DLG_ID_Main_BtnDeleteUnit,((count ACV_Unit_Stock) > 0)];
	};
};
ACV_FNCT_RefreshListObjects = {
	private ["_display","_list","_old_idx","_index","_varname","_action","_owner"];
	disableSerialization;
	// diag_log Format["%1: %2","ACV_FNCT_RefreshListObjects",_this];
	_display = findDisplay ACV_DLG_ID_Main_Dlg;
	if (_display != displayNull) then {
		if ((count _this) == 3) then {
			_varname = _this select 0;
			_action = _this select 1;
			_owner = _this select 2;
		} else {
			_varname = '';
			_action = -1;
			_owner = name player;
		};
		_list = _display displayCtrl ACV_DLG_ID_Main_ListObjects;
		_old_idx = lbCurSel _list;
		lbClear ACV_DLG_ID_Main_ListObjects;
		ACV_Object_Stock = Server getVariable "ACV_Object_Stock";
		{	_index = _list lbAdd (_x getVariable "Name");
			lbSetData [ACV_DLG_ID_Main_ListObjects,_index,(_x getVariable "VarName")];
			_list lbSetColor [_index,[1,0.9,0.1,1]];
		} foreach ACV_Object_Stock;
		if (_owner != (name player)) then {
			lbSetCurSel [ACV_DLG_ID_Main_ListObjects,_old_idx];
		} else {
			lbSetCurSel [ACV_DLG_ID_Main_ListObjects,_index];
		};
	};
};